Calculate the sum of a List of NumbersΒΆ
Calculate the sum of a List of Numbers.
def list_sum(L):
if len(L) == 1:
return L[0]
else:
return L[0] + list_sum(L[1:])
# test
print(list_sum([2, 4, 5, 6, 7])) # 24
def list_sum(L):
if len(L) == 1:
return L[0]
else:
return L[0] + list_sum(L[1:])
# test
print(list_sum([2, 4, 5, 6, 7])) # 24